// 1  引入  定义user 模块的store
// 2  创建 实例
// 3  导出 给组件 使用
// import router from '@/router'

import { defineStore } from 'pinia'

import router from '@/router'
export const useUserStore = defineStore('user', {
  state() {
    return {
      msg: 'hello pinia',
      user: '',
      tel: localStorage.getItem('tel') || '',
      count: 10,
      userid: localStorage.getItem('userid') || '',
    }
  },

  actions: {
    // 对 state 里面定义的数据进行操作   既可以 同步也可以异步

    setTel(payload: any) {
      this.tel = payload
      localStorage.setItem('tel', payload)
    },

    setUserId(payload: string) {
      this.userid = payload
      localStorage.setItem('userid', payload)
    },

    logout() {
      return new Promise((resolve) => {
        this.userid = ''
        this.tel = ''
        localStorage.removeItem('userid')
        localStorage.removeItem('tel')
        setTimeout(() => {
          resolve('成功')
        }, 1000)
      })
    },
    setUser(payload) {
      this.mytest = payload
    },

    delUser(payload = {}) {
      this.mytest = payload
    },
  },
  getters: {
    dubleCount() {
      return this.count * 2
    },
  },

  persist: true,
})
